import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 1.5
import QtQuick.Controls.Styles 1.4
import "../Process"

import "../global.js" as GlobalConst

Popup
{
    property Component compRadio: Component{RadioButton{exclusiveGroup: exGroup}}
    property var radios: []
    property QtObject pWord
    property alias message: message

    signal choiced(int answer)



    id: win
    //flags: Qt.FramelessWindowHint
    //modality: Qt.WindowModal
    width: 264
    height: 293
    visible: false
    //opacity: 1
    //color: Qt.rgba(0.5,0.5,0.5,0.9)


    Text {
        id: wordName
        //x: GlobalConst.Question_Text_Geometry[0]
        y: GlobalConst.Question_Text_Geometry[1]
        //width: GlobalConst.Question_Text_Geometry[2]
        height: GlobalConst.Question_Text_Geometry[3]
        //anchors.horizontalCenter: parent.horizontalCenter
        //x: (parent.width - width) / 2;

        font.pixelSize: 24;
        style: Text.Outline;
        styleColor: "lightgreen"
    }

    Text {
        id: message
        x: 0
        y: 0
        //width: GlobalConst.Question_Text_Geometry[2]
        //height: GlobalConst.Question_Text_Geometry[3]

        font.pixelSize: 16;
        style: Text.Outline;
        styleColor: "black"
    }

    ExclusiveGroup {
        id: exGroup

    }

    Column {
        id: radioRow
        x: 10
        y: 120
        width: 241
        height: 116
        spacing: 15
    }

/*
    RadioButton {
        id: radio0
        x: GlobalConst.Question_Explain_Pos[0][0]
        y: GlobalConst.Question_Explain_Pos[0][1]
        exclusiveGroup: exGroup
    }
    RadioButton {
        id: radio1
        x: GlobalConst.Question_Explain_Pos[1][0]
        y: GlobalConst.Question_Explain_Pos[1][1]
        exclusiveGroup: exGroup
    }
    RadioButton {
        id: radio2
        x: GlobalConst.Question_Explain_Pos[2][0]
        y: GlobalConst.Question_Explain_Pos[2][1]
        exclusiveGroup: exGroup
    }
    RadioButton {
        id: radio3
        x: GlobalConst.Question_Explain_Pos[3][0]
        y: GlobalConst.Question_Explain_Pos[3][1]
        exclusiveGroup: exGroup
    }
*/
    Progress {
        x: GlobalConst.Question_Progress_Pos[0]
        y: GlobalConst.Question_Progress_Pos[1]
        //width: 100
        //height: 10
        id: progress
        onTimeUp: {
            choiceAnswer(false)
        }
    }

    Button {
        id: buttonOK
        x: GlobalConst.Question_OK_Pos[0]
        y: GlobalConst.Question_OK_Pos[1]
        text: "ok"
        onClicked: choiceAnswer(true)
    }

    /*
    MouseArea {
        id: dragRegion
        anchors.fill: parent
        property point clickPos: "0,0"
        onPressed: {
            clickPos = Qt.point(mouse.x,mouse.y)
        }
        onPositionChanged: {
            //鼠标偏移量
            var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
            //如果mainwindow继承自QWidget,用setPos
            win.setX(win.x+delta.x)
            win.setY(win.y+delta.y)
        }
    }
    */

    function choiceAnswer(bChoiced) {
        progress.stop();
        win.hide()

        if(bChoiced) {
            pWord.userAnswer = -1;
            for(var i = 0; i < 4; i++) {
                if(radios[i].checked == true) {
                    pWord.userAnswer = i;
                    //console.debug("userAnswer:",i,pWord.userAnswer)
                    break;
                }
            }
        }
        choiced(pWord.userAnswer)
    }

    function initRadios(n) {

        for(var i = 0; i < 4; i++) {
            if(n != i)radios[i].checked = false;
            else radios[i].checked = true;
        }
        /*
        if(showType != 0)radio0.checked = false;
        else radio0.checked = true;

        if(showType != 1)radio1.checked = false;
        else radio1.checked = true;

        if(showType != 2)radio2.checked = false;
        else radio2.checked = true;

        if(showType != 3)radio3.checked = false;
        else radio3.checked = true;
        */
    }

    function showQuestion(w,answer) {
        var iTime
        pWord = w
        wordName.text = pWord.word

        for(var i = 0; i < 4; i++) {
            //console.debug(word.explain[i])
            radios[i].text = pWord.explain[i];
        }
        /*
        radio0.text = word.answers[0]
        radio1.text = word.answers[1]
        radio2.text = word.answers[2]
        radio3.text = word.answers[3]
        */

        if(answer < 4 && answer >= -1) {    //显示选择
            /*
            radio0.enabled = false;
            radio1.enabled = false;
            radio2.enabled = false;
            radio3.enabled = false;
            */

            for(var i = 0; i < 4; i++) {
                radios[i].enabled = false;
            }

            buttonOK.enabled = false;
            iTime = GlobalConst.Question_ShowChoice_Time
            initRadios(answer)
        }
        else if(answer == -2) { //选择

            for(var i = 0; i < 4; i++) {
                radios[i].enabled = true;
            }
            /*
            radio0.enabled = true;
            radio1.enabled = true;
            radio2.enabled = true;
            radio3.enabled = true;
            */
            buttonOK.enabled = true;
            iTime = GlobalConst.Question_Choice_Time
            initRadios(answer)
        }
        win.show()
        progress.start(iTime,false)
    }

    //onClosing: {
    //    close.accepted = false
    //}

    Component.onCompleted: {
        //word = {"word":"word","ph":"ph","answers":["a","b","c","d"],"iAnswer":1}
        //show(0)
        for(var i = 0; i < 4; i++) {
            radios.push(compRadio.createObject(win,{"parent": radioRow}))
            radios[i].x = GlobalConst.Question_Explain_Pos[i][0]
            radios[i].y = GlobalConst.Question_Explain_Pos[i][1]
        }

        //win.setAttribute(Qt.WA_TranslucentBackground);
        //win.setStyleSheet("background:transparent;");

    }
}

/*
  Text;TextInput;TextEdit;TextField;TextArea;
  */
